using Microsoft.EntityFrameworkCore;
using StudentManagement.Domain.Entities;
using StudentManagement.Domain.Repositories;
using StudentManagement.Infrastructure.Data;

namespace StudentManagement.Infrastructure.Repositories;

public class CourseRepository : Repository<Course>, ICourseRepository
{
    public CourseRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<IEnumerable<Course>> GetCoursesByClassIdAsync(int classId)
    {
        return await _dbSet.Where(c => c.ClassId == classId).ToListAsync();
    }

    public async Task<Course> GetCourseByCodeAsync(string courseCode)
    {
        return await _dbSet.FirstOrDefaultAsync(c => c.CourseCode == courseCode);
    }

    public async Task<IEnumerable<Course>> SearchCoursesAsync(string keyword)
    {
        if (string.IsNullOrWhiteSpace(keyword))
        {
            return await GetAllAsync();
        }

        keyword = keyword.ToLower();
        return await _dbSet
            .Where(c => c.CourseCode.ToLower().Contains(keyword) ||
                        c.CourseName.ToLower().Contains(keyword) ||
                        c.Description.ToLower().Contains(keyword) ||
                        c.TeacherName.ToLower().Contains(keyword))
            .ToListAsync();
    }

    public async Task<IEnumerable<Course>> GetCoursesWithScoresAsync()
    {
        return await _dbSet.Include(c => c.Scores).ToListAsync();
    }
}